package com.zut.dao;

import com.zut.domain.Evaluation;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface EvaluationDao {

    @Results(id = "groupWithEvaluation",
            value = {
                    @Result(property = "userId", column = "user_id", id = true),
                    @Result(property = "evaluationId", column = "evaluation_id", id = true),
                    @Result(property = "coachId", column = "coach_id"),
                    @Result(property = "grade", column = "evaluation_grade"),
                    @Result(property = "content", column = "evaluation_content"),
                    @Result(property = "faceless", column = "faceless"),
                    @Result(property = "date", column = "evaluation_date")
            })
    @Select("select * from evaluation")
    List<Evaluation> getAll();

    @Insert("insert into evaluation (user_id,evaluation_id,coach_id,evaluation_grade,evaluation_content,faceless,evaluation_date) " +
            "values(#{userId},#{evaluationId},#{coachId},#{grade},#{content},#{faceless},#{date})")
    int insert(Evaluation evaluation);

    @Delete("delete from evaluation where user_id = #{userId} and evaluation_id = #{evaluationId})")
    int deleteById(String userId,String evaluationId);
    @Update("<script>                                                                "
            + "update evaluation                                                     "
            + "<set>                                                                 "
            + "  <if test='grade != null'> evaluation_grade=#{grade},           </if>"
            + "  <if test='content != null'> evaluation_content=#{content},     </if>"
            + "  <if test='faceless != null'> faceless=#{faceless},             </if>"
            + "  <if test='date != null'> evaluation_date=#{date},              </if>"
            + "</set>                                                                "
            + "where user_id = #{userId} and evaluation_id = #{evaluationId}         "
            + "</script>                                                             ")
    int update(Evaluation evaluation);

    @ResultMap("groupWithEvaluation")
    @Select("select * from evaluation where coach_id = #{id}")
    List<Evaluation> getById(String id);

    @Select("select user_id , count(exam_grade >= 80) as subjectNum from exam_result group by user_id")
    List<Map<String,String>> getAvgGrade();
}
